﻿//注意：在C# 9.0+中，顶级语句必须位于所有类型声明之前
//在C# 9.0及以上版本中，顶级语句 是一项简化代码的语法特性。
// 它允许你直接在文件中编写执行代码，
// 而无需显式定义 Main 方法和 Program 类。
//顶级语句是指直接写在文件中的执行代码，它们会被编译器自动包装成一个 Main 方法，作为程序的入口点。
Person p = new Person { Name = "Alice", Age = 25 };
string description = p switch
{
    { Age: < 18 } => "未成年人",
    { Age: >= 18 and < 60 } => "成年人",
    { Age: >= 60 } => "老年人",
    _ => "未知"
};
Console.WriteLine(description);

//do while和while对比
int count = 0;
while (count < 3)
{
    Console.WriteLine("Hello, while循环！");
    count++;
}
int count1 = 0;
do
{
    Console.WriteLine("Hello, do-while循环！");
    count1++;
} while (count < 3);//换成0

//foreach
//遍历数组
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int num in numbers)
{
    Console.WriteLine(num);
}
//遍历列表
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };
foreach (string fruit in fruits)
{
    Console.WriteLine(fruit);
}

//遍历字典
Dictionary<string, int> ages = new Dictionary<string, int>
{
    { "Alice", 25 },
    { "Bob", 30 },
    { "Charlie", 35 }
};
foreach (var kvp in ages)
{
    Console.WriteLine($"{kvp.Key} 的年龄是 {kvp.Value} 岁");
}

//遍历字符串中的字符
string message = "Hello";
foreach (char c in message)
{
    Console.WriteLine(c);
}

//嵌套循环99乘法表
for (int i = 1; i <= 9; i++) // 外层循环控制行
{
    for (int j = 1; j <= i; j++) // 内层循环控制列
    {
        Console.Write($"{j} * {i} = {i * j}\t"); // 打印乘法表达式
    }
    Console.WriteLine(); // 换行
}

//1.类型模式
object obj = "Hello";  
switch (obj)  
{  
    case int i:  
        Console.WriteLine($"整数: {i}");  
        break;  
    case string s:  
        Console.WriteLine($"字符串: {s}");  
        break;  
    case null:  
        Console.WriteLine("空值");  
        break;  
    default:  
        Console.WriteLine("未知类型");  
        break;  
}  
//2.变量模式
object obj1 = 42;
switch (obj1)
{
    case var x when x is int && (int)x > 30:
        Console.WriteLine("大于30的整数");
        break;
    default:
        Console.WriteLine("其他情况");
        break;
}
//3.范围模式
int score = 85;
switch (score)
{
    case int s when s >= 90:
        Console.WriteLine("优秀");
        break;
    case int s when s >= 80 && s < 90:
        Console.WriteLine("良好");
        break;
    default:
        Console.WriteLine("其他");
        break;
}
//4.属性模式
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

//在最上
// Person p = new Person { Name = "Alice", Age = 25 };
// string description = p switch
// {
//     { Age: < 18 } => "未成年人",
//     { Age: >= 18 and < 60 } => "成年人",
//     { Age: >= 60 } => "老年人",
//     _ => "未知"
// };
//Console.WriteLine(description);

//do-while

// do
// {
//     Console.WriteLine("Hello, do-while循环！");
//     count++;
// } while (count < 3);

